import domain.Major;
import mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class ClassText {
    private static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        // 1. 定义配置文件的位置
        String resource = "mybatis-config.xml";
        // 2. 获取这个文件的输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 3. 基于配置文件构建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "dev");
    }

    @Test
    public void selectClass() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Major c = studentMapper.selectClass(2);
            System.out.println(c);
        }
    }

    @Test
    public void selectClass2() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            List<Major> list = studentMapper.selectMajors();
            for (Major m : list) {
                System.out.println(m);
            }
        }
    }
}
